<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchMap.data" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchMap.data.name"
          placeholder="内容名"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchTable()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="attractions=[];content={};status=false;scenic={};addContent();">添加内容</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="contenList" style="width: 100%">
      <el-table-column prop="name" label="内容名称">
      </el-table-column>
      <el-table-column prop="" label="内容图片">
        <template slot-scope="scope">
            
            <el-image
                style="width: 100px; height: 100px"
                :src="scope.row.img" >
            </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="" label="是否启用">
        <template slot-scope="scope">
          <el-switch
            @change="update(scope.row)"
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <el-button type="warning" round @click="delContent(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="内容添加" :visible.sync="dialogFormVisible">
      <el-form>
            <el-form-item label="选择景区" :label-width="'120px'">
                <el-select v-model="scenic" placeholder="请选择景点" @change="changeOne">
                    <el-option :label="item.name" :value="item.id"  v-for="item in attractions" :key="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="内容名称" :label-width="'120px'">
                <el-input v-model="content.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="内容图片" :label-width="'120px'">
                <img :src="content.img">
            </el-form-item>
            <el-form-item label="是否启用" :label-width="'120px'">
                <el-switch
                    @click="status=!status"
                    v-model="status"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                >
                </el-switch>
            </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false;saveContent()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      style="float: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.total"
    >
    </el-pagination>
  </div>
</template>
<script>
import { search, updateContent, delContent,findAll } from "@/api/content";
export default {
  data() {
    return {
        total: 0,
        searchMap: {
            page: 1,
            limit: 5,
            data: {},
        },
        contenList: [], //内容信息
        dialogFormVisible:false,//添加弹窗开关
        attractions:[],//景区详情
        scenic:{},
        content:{},
        status:false

    };
  },
  methods: {
    //搜索
    searchTable() {
      var _this = this;
      search(this.searchMap).then((res) => {
        _this.contenList=res.data
        _this.total = res.total;
        _this.contenList.forEach(a=>{
                if(a.status==1){
                    a.status=true
                }else{
                    a.status=false
                }

                if(a.status==1){
                    a.status=true
                }else{
                    a.status=false
                }
            })
      });
    },
    //渲染
    changeOne(row){
        this.attractions.forEach(element=>{
            if(row==element.id){
                this.content.sdId=row
                this.content.name=element.name
                this.content.img=element.img
            }
            this.searchTable();
        })
    },
    //添加
    addContent(){
        this.dialogFormVisible=true;
        var _this=this
        findAll().then(res=>{
            res.forEach(element => {
                _this.attractions.push(element)
            });
            this.searchTable()
        })
    },
    //保存
    saveContent(){
        _this=this
        this.content.status=this.status?1:0
        updateContent(this.content).then(res=>{
            if (res.success) {
                _this.searchTable();
                this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "success",
                });
            } else {
                this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "error",
                });
            }
            _this.searchTable();
        })
    },
    //修改
    update(row){
        console.log(row)
        row.status=row.status?1:0
        updateContent(row).then(res=>{
            if (res.success) {
                this.searchTable();
                this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "success",
                });
            } else {
                this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "error",
                });
            }
            this.searchTable();
        })
    },
    //删除
    delContent(row){

        this.$confirm('确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            delContent(row).then(res=>{
                if (res.success) {
                this.searchTable();
                this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "success",
                });
            } else {
                this.$message({
                    showClose: true,
                    message: res.msg,
                    type: "error",
                });
            }
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
        this.searchTable();
        
    },
    handleSizeChange(size) {
      this.searchMap.limit = size;
      this.searchTable();
    },
    handleCurrentChange(currentPage) {
      this.searchMap.page = currentPage;
      this.searchTable();
    },
  },
  created() {
    let that = this;
    that.searchTable();
    if(!localStorage.getItem("token")){
        location.href="/#/login"
    }
  },
};
</script>